package cn.edu.sjtu.spinning.controller;

import cn.edu.sjtu.spinning.domain.Task;
import cn.edu.sjtu.spinning.dto.TaskInput;
import cn.edu.sjtu.spinning.enums.RespStatus;
import cn.edu.sjtu.spinning.service.SpinningService;
import cn.edu.sjtu.spinning.util.ResultUtil;
import cn.edu.sjtu.spinning.vo.Result;
import cn.edu.sjtu.spinning.vo.TasksWithTypeVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author loumoon
 * @date 2019-11-20 10:02
 */
@RestController
@Slf4j
public class SpinningController {

  private final SpinningService spinningService;

  public SpinningController(@Qualifier("WithoutDevice") SpinningService spinningService) {
    this.spinningService = spinningService;
  }

  @GetMapping(value = "/api/state")
  public Result getStatus() {
    log.info("without params");
    return ResultUtil.success(spinningService.queryECNState());
  }

  /*查询状态, 有参数*/
  @GetMapping(
      value = "/api/state",
      params = {"processId", "taskId"})
  public Result getStatus(String processId, String taskId) {
    log.info("with params {} {}", processId, taskId);
    return ResultUtil.success(spinningService.queryTaskState(processId, taskId));
  }

  /*加入待处理队列*/
  @PostMapping(value = "/api/offer")
  public Result addTaskToQueue(@RequestBody TaskInput taskInput) {
    boolean ret = spinningService.addTaskToQueue(taskInput);
    if (ret) {
      return ResultUtil.success();
    }
    return ResultUtil.error(RespStatus.TASK_OFFER_ERROR);
  }

  @GetMapping("/api/tasks")
  public Result listTaskQueue() {
    //    List<TaskVO> taskVOQueue = flawDetectionService.listTaskQueue();
    //    return ResultUtil.success(taskVOQueue);
    Map<Integer, List<Task>> map = spinningService.retTasksPool();
    return ResultUtil.success(TasksWithTypeVO.build(map));
  }

  /*数据接口*/
  /*edit by hashao*/

  // 输入数据
  // 是主动去TDM等系统拉数据，还是通过这个接口输入数据
  @PostMapping(value = "input")
  public Result inputData(Object o) {
    return ResultUtil.success();
  }

  // 输出数据，通过这个接口拉取输出数据？还是通过某种机制发送到data topic上
  @GetMapping(value = "output")
  public Result outputData() {
    return ResultUtil.success();
  }

  // 录入输出数据，输出数据如何产生的，
  // 是通过这个接口录入数据，
  // 还是通过这个接口去某个地方拉取数据
  @PostMapping(value = "typeData")
  public Result typeData(Object o) {
    return ResultUtil.success();
  }
}
